import { defineStore } from "pinia";
import {
  ACCESS_TOKEN,
  CURRENT_USER,
} from "@/store/mutation-types";

import { storage } from "@/utils/lib/Storage";
import { getCode, register, login, update, getUserInfo, userFeedback } from "@/api/system/user";
import { useRouter } from "vue-router";



interface IUserState {
  token: string;
  info: any;
}

export const useUserStore = defineStore({
  id: "app-user",
  state: (): IUserState => ({
    token: storage.get(ACCESS_TOKEN, ""),
    info: storage.get(CURRENT_USER, {}),
  }),
  getters: {
    getToken(): string {
      return this.token;
    },
    getUserInfo(): object {
      return this.info;
    },
    getUserSex(): string {
      return this.info.sex;
    },
    getUserSignature(): string {
      return this.info.signature;
    },
  },
  actions: {
    setToken(token: string) {
      this.token = token;
    },

    setUserInfo(info) {
      this.info = info;
    },
    setInfoSex(sex: string) {
      this.info.sex = sex;
    },
    setInfoHead(head: string) {
      this.info.head = head;
    },
    setInfoMoneyCode(money_code: string) {
      this.info.money_code = money_code;
    },
    setInfoSignature(signature: string) {
      this.info.signature = signature;
    },

    // 注册
    async register(data) {
      try {
        const res = await register(data);
      } catch (error) { }
    },
    // 登录
    async login(data) {
      try {
        const res = await login(data);
        if (res.data.code === 200) {
          const ex = 7 * 24 * 60 * 60;
          storage.set(ACCESS_TOKEN, res.data.data.token, ex);
          this.setToken(res.data.data.token);
        }
        return Promise.resolve(res);
      } catch (error) {
        return Promise.reject(error);
      }
    },
    // 获取验证码
    async getEamilCode(data) {
      try {
        const res = await getCode({
          email: data.email,
        });
        return Promise.resolve(res);
      } catch (error) {
        return Promise.reject(error);
      }
    },
    // 更新用户信息
    async updateInfo(data) {
      try {
        console.log(111);

        await update(data);
        const ex = storage.get(CURRENT_USER, {}).expire;
        this.setInfoSex(data.sex);
        this.setInfoSignature(data.signature);
        this.setInfoHead(data.head);
        this.setInfoMoneyCode(data.money_code);
        storage.set(CURRENT_USER, this.info, ex);
        console.log("this.info", this.info);
      } catch (error) {
        return Promise.reject(error);
      }
    },
    // 获取用户信息
    async getInfo(data) {
      try {
        const res = await getUserInfo(data);
        if (res.data.code === 200) {
          const ex = 7 * 24 * 60 * 60;
          storage.set(CURRENT_USER, res.data.data, ex);
          this.setUserInfo(res.data.data);
        }
        return Promise.resolve(res);
      } catch (error) {
        return Promise.reject(error);
      }
    },
    // 反馈
    async feedback(data) {
      try {
        await userFeedback(data)
      } catch (error) {
        return Promise.reject(error);
      }
    }
  },
});